<?php
namespace addons\shopro\icbc;

use addons\shopro\icbc\ICBCPayBizContentParameter;
use Exception;
include_once 'DefaultIcbcClient.php';
include_once 'ICBCEPayBizContentParameter.php';
include_once 'ICBCPayBizContentParameter.php';
include_once 'UiIcbcClient.php';
/**
 * Class ICBCPay
 * 工银支付
 */
class ICBCPay
{

    private $client;

    private $payType;

    /**
     * ICBCPay constructor.
     * @param string $payType 9 微信 10 支付宝 11 e支付
     */
    public function __construct($payType)
    {
        $icbc_app_id="11000000000000016036";
        $icbc_private_key="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCssav8hbwKkjVxj3nojQ6HHe7+cCvWPNvbQFDzYJtIRetqxW3AzSB1Gfe7J8MgzMy4BnIWJRiIzEqapxxeFo2oJ7clILY1gkmyyYfFFQLNX0Fooy8sv+EJ2GxHZr4io5Idy0ALN1ctPXFVZaI1OSxn8N06MixQJImzo3ObHVkCHOtzifUAeH+rDYh5XKACQ1dcACi2YPibZdm3rK67Xuguzmbf2NnEJIjMhI/vbq50n0eR+601r9t64BuKqBgu5UXKyzPHgcN3OEbJZQ0ScvKYfguAHNy/E3UM0r8TkceF9WtzuGzGSmOO3BLnT01xz/VajkXgAIzYHVa35kNKbw8LAgMBAAECggEAVFZ4zdxqFyeMXidZvA6XV7lwrGYPopMaNBtW7WTysPskm4JLtWr4PPgvmgEzNGOQXxcWxGXdc4GBtcGDAxg0mji7IrC1C7DTWTM+0SsPsUih4v4+3bXfqzU8vCobFUOY6mAGqPOf6YImRTRbYW7IlwJ0qCU0qvcNIOPkY46Tl4nGZ4/PNBNM7Yb6F5Eq4zC8ANJwnHXHs7VExf9N/bNELezEnOIuqjKuAUa7rlh++0ma/iTjGOgvDBj1RhRgMkbeQsgK18xzv5ZUG7+70ZNK5q97vNrXYKHSDzcrp1hUTgZlMGOOqhHUm20EF2syol9R+5QIjAjmY+S6qhYbXn/IgQKBgQDGrE6uhs5S6XcUZAUC7zzV/uRTx+3hqlc4CJs1ha8nC9QpHZHlhD6z8ybHe2lAy8biS9Qc+XSDZCk1naKVanMtt4sh31MSBHaQ4Lg0jb6LhDCHDesgM2+t7/NTiX7knAj28gBrfagBWAltr45F6qUxhX4WOPAvjB3sJI4xqmPuawKBgQDehlR6/ow8hcqrs1m8zzuvkQwkq1bKJOVKnjkIviWa9VCCPq3yR2fWYplEqUowWEOCX+UJt9isymSI3O5syEKstMUCDiIG87EfxsNG61EKnefqwptIaVUloXpgRfmEl7WfqQynGHq4v3TLeqi+22hZv701bU2dEhBJ5zXKqFpJ4QKBgQC/fEgB0xB9vWP2lUh3QZrDLv9m+OYAgSu7aCAggA8HdVExp7x9GO9Ohec0kXAq8b1pU2jH3G8+TIWjlaGknYsR3zwMk7D/Rqn4jX89t/iVg/05zmJ3ENxtKz69n2JceT/ys+FPuTb6xJ2hyHk6TiSWDHIwfQSS71vPxJQArpfOrQKBgHEjaUaTr2eCOtZrvN+DmfwcykjGmLz0X2Q96A+q5eHQgluGVNiPN5tHzFednyvPZJuf+Aql39OSn+T6N3s1ori+vD/cQa65LkBSxayELlMwj97LjToW3ATjAh561W911xFTWSyIBLV0GoTuW2eWlIq1MxU7+q82pPN+S9o7GJThAoGAdUM1lApxS+Zn4p0MDIKzoIHNE5JkNXJFwxp4o0cbnfyEEZqdVruX80D8hyKgPDnYuCeDLBqnKBkrdfq8jNTFDEBmFk+V4+HRj/7aL2WXKOkW3O1AOz+yjTHhSFwc4He1jKCAz585aKWQG1TWPHlnp2QzGlqpsD/W3tIjO9etYIc=";
        $icbc_sign_type="RSA2";
        $icbc_charset="UTF-8";
        $icbc_format="json";
        $icbc_gateway_public_key="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuRgTG1jjeO39CIMB/gRK
UmiBLkXIHrMbJC/MGAF2/k8BLc6hk+jdDZHJ7aO+QBYyku4LXwDSs35I47dlZdY8
GsifC+gleTNvJuzKxhMu4u3v9Qbb8/RkJabbklAY9JefUD0fdLMEF70TkYLO5X4M
LLjcIspkpftn1SxKF53jLh1fKGdcyP+fwSw+ZUClVw6Xmp63dqwRt2cyO3l5yMW7
z60H218pFvuAJy1rePyhGXoP9wC8a0XXk3SyEQjNT6rxSRhgJB365COMsFFiLi6J
8/peMtXvsRLAFNoDXlXy9mQaiWAb3vjv2kaS9SRpTImR7ulDj/xVSYhBZCQ4uoxS
AwIDAQAB";
        $icbc_encryptKey='n3PkFbIHQv3H9vZ687FZqQ==';//"n3PkFbIHQv3H9vZ687FZqQ==";
        $icbc_encryptType = "AES";
        $this->client = new \DefaultIcbcClient($icbc_app_id, $icbc_private_key, $icbc_sign_type, $icbc_charset, $icbc_format, $icbc_gateway_public_key, $icbc_encryptKey, $icbc_encryptType, "", "");
        /*if ($payType == "9" || $payType == "10" || !$payType) {
            $this->client = new \DefaultIcbcClient($icbc_app_id, $icbc_private_key, $icbc_sign_type, $icbc_charset, $icbc_format, $icbc_gateway_public_key, $icbc_encryptKey, $icbc_encryptType, "", "");
        } elseif ($payType == "11") {
            $this->client = new \UiIcbcClient($icbc_app_id, $icbc_private_key, $icbc_sign_type, $icbc_charset, $icbc_format, $icbc_gateway_public_key, $icbc_encryptKey, $icbc_encryptType, "", "");
        }*/
    }

    /**
     * app 支付宝和微信下单
     * 张志强
     * 2021-01-12
     * @param string $subMerchantId 商户号
     * @param string $mer_prtcl_no 协议编号
     * @param string $orderId 订单号
     * @param string $money 金额
     * @param string $decive 设备号
     * @param string $attach 附加字段
     * @param string $clientIp 设备ip
     * @param string $notifyUrl 回调地址
     * @return false|string
     * @throws Exception
     */
    public function appPay($subMerchantId, $mer_prtcl_no, $orderId, $money, $decive, $attach, $clientIp, $notifyUrl)
    {
        if ($this->payType == 11) {
            throw new Exception("请使用e支付的接口");
        }
        $data = [
            'serviceUrl' => 'https://gw.open.icbc.com.cn/api/cardbusiness/aggregatepay/b2c/online/consumepurchase/V1',
            'biz_content' => get_object_vars(new ICBCPayBizContentParameter($subMerchantId, $mer_prtcl_no, $orderId . "", 9, $decive, $money . "", $attach, $clientIp, $notifyUrl)),
            'extraParams' => [],
            'method' => "POST",
            'isNeedEncrypt' => true
        ];
        dump(json_encode($data));die();

        return $this->client->execute($data, $orderId, "");
    }

    /**
     * e支付 h5
     * 张志强
     * 2021-01-12
     * @param string $subMerchantId 商户号
     * @param string $mer_prtcl_no 协议编号
     * @param string $orderId string     订单号
     * @param string $money string      金额
     * @param string $attach string     附加字段
     * @param boolean $isInApp bool 是否嵌入app内
     * @param string $notifyUrl 回调地址
     * @param string $returnUrl 支付完跳转地址
     * @return string
     * @throws Exception
     */
    public function ePay($subMerchantId, $mer_prtcl_no, $orderId, $money, $attach, $isInApp, $notifyUrl, $returnUrl)
    {
        if ($this->payType != 11) {
            throw new Exception("请使用聚合支付的接口");
        }
        $data = [
            'serviceUrl' => 'https://gw.open.icbc.com.cn/ui/cardbusiness/epayh5/ui/consumption/V1',
            'biz_content' => get_object_vars(new ICBCEPayBizContentParameter($subMerchantId, $mer_prtcl_no, $orderId . "", $money . "", $attach, $isInApp, $notifyUrl, $returnUrl)),
            'extraParams' => [],
            'method' => "POST",
            'isNeedEncrypt' => true
        ];

        return $this->client->buildPostForm($data, $orderId, "");
    }

    /**
     * 退款
     * 张志强
     * 2021-01-12
     * @param string $subMerchantId 商户编号
     * @param string $refundId 自己生成的唯一退款id
     * @param string $orderId 订单号
     * @param string $money 金额
     * @return false|string
     * @throws Exception
     */
    public function refund($subMerchantId, $refundId, $orderId, $money)
    {
        // 退款
        $data = [
            'serviceUrl' => 'https://gw.open.icbc.com.cn/api/cardbusiness/aggregatepay/b2c/online/merrefund/V1',
            'biz_content' => get_object_vars(new ICBCRefundBizContentParameter($subMerchantId, $refundId . "", $orderId . "", $money . "")),
            'extraParams' => [],
            'method' => "POST",
            'isNeedEncrypt' => true
        ];
        return $this->client->execute($data, $orderId, "");
    }

    // 查询订单

    /**
     * @param $subMerchantId
     * @param $serial_number
     * @return false|string
     * @throws Exception
     */
    public function selectIcbcOrder($subMerchantId, $serial_number)
    {
        $data = [
            'serviceUrl' => 'https://gw.open.icbc.com.cn/api/cardbusiness/aggregatepay/b2c/online/orderqry/V1',
            'biz_content' => [
                'mer_id' => $subMerchantId,
                'out_trade_no' => $serial_number,
                'deal_flag' => "0",
                'icbc_appid' => C('icbc_app_id')
            ],
            'extraParams' => [],
            'method' => "POST",
            'isNeedEncrypt' => true
        ];

        return $this->client->execute($data, microtime(), "");
    }
}
